package com.study.shop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.study.shop.entity.dto.ValueDTO;
import com.study.shop.entity.po.Value;
import com.study.shop.mapper.ValueMapper;
import com.study.shop.service.ValueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ValueServiceImpl implements ValueService {
    private final ValueMapper valueMapper;

    @Override
    public List<Value> saveValueList(List<ValueDTO> valueList) {
        // 避免传入空集合
        if (CollectionUtil.isEmpty(valueList)) {
            return new ArrayList<Value>();
        }
        // 属性copy
        List<Value> values = valueList.stream().map(dto -> {
            Value v = new Value();
            v.setName(dto.getValueName());
            return v;
        }).collect(Collectors.toList());
        // 插入数据
        valueMapper.insertBatch(values);
        return values;
    }

    @Override
    public int deleteValuesByIds(List<Integer> vIds) {
        return valueMapper.deleteValuesByIds(vIds);
    }
}
